package gatech.cs3251.project4;

public class Header {
	public static final int size = 8;
	
	public MessageType type;
	public int id;
	public int length;
	
	public Header(MessageType type, int id, int length) {
		this.type = type;
		this.id = id;
		this.length = length;
	}
	
	public Header() {
		this(MessageType.LEAVE,0,0);
	}
	
	/**
	 * Parses a String into a Header
	 * @param s String to parse
	 * @return Parsed Header
	 */
	public static Header parseHeader(String s) {
		Header h = new Header();
		
		if(s.length() < 8) return null;
		
		h.type = MessageType.convert(s.charAt(0));
		h.id = (int)s.charAt(2) + s.charAt(3);
		h.length = (int)s.charAt(4) + s.charAt(5) + s.charAt(6) + s.charAt(7);
		
		return h;
	}
	
	@Override
	public String toString() {
		return"";
	}
	
	public String networkPrint() {
		String header = "";
		header += (char)(type.ordinal())&0xff; //type
		header += '\0'; //z1
		header += (char)(id>>24)&0xff; //length 1
		header += (char)(id>>16)&0xff; //length 2
		header += (char)(id>>8)&0xff; //length 3
		header += (char)id&0xff; //length 4
		return header;
	}
}
